<!-- templates/inventory/instock.html -->
<!DOCTYPE html>
<html lang="zh-CN">

<head>
    <meta charset="UTF-8">
    <meta name="viewport" content="width=device-width, initial-scale=1.0">
    <title>商店进销存管理系统</title>
    <script src="https://cdn.tailwindcss.com"></script>
    <link href="https://cdn.jsdelivr.net/npm/font-awesome@4.7.0/css/font-awesome.min.css" rel="stylesheet">

    <!-- 从 unpkg CDN 引入 ECharts -->
    <script src="https://unpkg.com/echarts@5.4.0/dist/echarts.min.js"></script>

    <!-- 引入 Toastify CSS 和 JS -->
    <script src="https://cdn.jsdelivr.net/npm/sweetalert2@11"></script>
    <link href="https://cdn.jsdelivr.net/npm/toastify-js/src/toastify.min.css" rel="stylesheet">
    <script src="https://cdn.jsdelivr.net/npm/toastify-js/src/toastify.min.js"></script>

    <!-- Tailwind 配置 -->
    <script>
        tailwind.config = {
            theme: {
                extend: {
                    colors: {
                        primary: '#165DFF',
                        secondary: '#0FC6C2',
                        success: '#00B42A',
                        warning: '#FF7D00',
                        danger: '#F53F3F',
                        info: '#86909C'
                    },
                    fontFamily: {
                        inter: ['Inter', 'system-ui', 'sans-serif'],
                    },
                },
            }
        }
    </script>

    <!-- 自定义工具类 -->
    <style type="text/tailwindcss">
        @layer utilities {
            .content-auto {
                content-visibility: auto;
            }

            .sidebar-item {
                @apply flex items-center px-4 py-3 text-gray-600 hover:bg-gray-50 hover:text-primary transition-all duration-200;
            }

            .sidebar-item.active {
                @apply bg-primary/10 text-primary border-l-4 border-primary;
            }

            .card-shadow {
                @apply shadow-md hover:shadow-lg transition-shadow duration-300;
            }
            .input-focus {
                @apply focus:outline-none focus:ring-2 focus:ring-primary/30 focus:border-primary;
            }
            .btn-primary {
                @apply bg-primary text-white px-4 py-2 rounded-md hover:bg-primary/90 transition-colors;
            }
            .btn-secondary {
                @apply bg-gray-100 text-gray-600 px-4 py-2 rounded-md hover:bg-gray-200 transition-colors;
            }
            .btn-danger {
                @apply bg-danger text-white px-4 py-2 rounded-md hover:bg-danger/90 transition-colors;
            }
        }
    </style>
</head>

<body class="font-inter bg-gray-50 min-h-screen flex flex-col">
<!-- 顶部导航栏 -->
<header class="bg-white shadow-sm">
    <div class="container mx-auto px-4 py-3 flex justify-between items-center">
        <div class="flex items-center space-x-2">
            <i class="fa fa-cubes text-primary text-2xl"></i>
            <h1 class="text-xl font-bold text-gray-800">商店进销存管理系统</h1>
        </div>

        <div class="flex items-center space-x-6">
            <div class="relative">
                <input type="text" placeholder="搜索..."
                       class="pl-10 pr-4 py-2 rounded-full border border-gray-300 input-focus">
                <i class="fa fa-search absolute left-3 top-1/2 transform -translate-y-1/2 text-gray-400"></i>
            </div>

            <div class="flex items-center space-x-4">
                <button class="text-gray-600 hover:text-primary transition-colors">
                    <i class="fa fa-bell-o text-xl"></i>
                </button>
                <button class="text-gray-600 hover:text-primary transition-colors">
                    <i class="fa fa-cog text-xl"></i>
                </button>
                <div class="flex items-center space-x-2 cursor-pointer">
                    <img src="https://picsum.photos/200/200" alt="用户头像" class="w-8 h-8 rounded-full object-cover">
                    <span class="text-gray-700">管理员</span>
                </div>
            </div>
        </div>
    </div>
</header>

<div class="flex flex-1 overflow-hidden">
    <!-- 侧边栏导航 -->
    <aside class="w-64 bg-white shadow-md h-[calc(100vh-64px)] overflow-y-auto transition-all duration-300">
        <nav class="py-4">
            <div class="px-4 mb-4">
                <h2 class="text-xs font-semibold text-gray-400 uppercase tracking-wider">主菜单</h2>
            </div>

            <a href="../index.html" class="sidebar-item" data-path="index.html">
                <i class="fa fa-tachometer w-5 h-5 mr-3"></i>
                <span>仪表盘</span>
            </a>

            <div class="px-4 mt-6 mb-2">
                <h2 class="text-xs font-semibold text-gray-400 uppercase tracking-wider">基础信息</h2>
            </div>

            <a href="productlist.html" class="sidebar-item" data-path="productlist.html">
                <i class="fa fa-shopping-bag w-5 h-5 mr-3"></i>
                <span>商品管理</span>
            </a>

            <a href="categorylist.html" class="sidebar-item" data-path="categorylist.html">
                <i class="fa fa-th-large w-5 h-5 mr-3"></i>
                <span>类别管理</span>
            </a>

            <a href="supplierlist.html" class="sidebar-item" data-path="supplierlist.html">
                <i class="fa fa-truck w-5 h-5 mr-3"></i>
                <span>供应商管理</span>
            </a>


            <div class="px-4 mt-6 mb-2">
                <h2 class="text-xs font-semibold text-gray-400 uppercase tracking-wider">库存管理</h2>
            </div>

            <a href="warehouselist.html" class="sidebar-item" data-path="warehouselist.html">
                <i class="fa fa-archive w-5 h-5 mr-3"></i>
                <span>仓库管理</span>
            </a>

            <a href="inventorylist.html" class="sidebar-item" data-path="inventorylist.html">
                <i class="fa fa-list-alt w-5 h-5 mr-3"></i>
                <span>库存查询</span>
            </a>

            <a href="instock.html" class="sidebar-item " data-path="instock.html">
                <i class="fa fa-sign-in w-5 h-5 mr-3"></i>
                <span>入库管理</span>
            </a>

            <a href="outstock.html" class="sidebar-item" data-path="outstock.html">
                <i class="fa fa-sign-out w-5 h-5 mr-3"></i>
                <span>出库管理</span>
            </a>

            <a href="transfer.html" class="sidebar-item" data-path="transfer.html">
                <i class="fa fa-exchange w-5 h-5 mr-3"></i>
                <span>转仓管理</span>
            </a>

            <div class="px-4 mt-6 mb-2">
                <h2 class="text-xs font-semibold text-gray-400 uppercase tracking-wider">统计分析</h2>
            </div>

            <a href="statistics/sales.html" class="sidebar-item" data-path="statistics/sales.html">
                <i class="fa fa-line-chart w-5 h-5 mr-3"></i>
                <span>销售统计</span>
            </a>

            <a href="statistics/inventory.html" class="sidebar-item" data-path="statistics/inventory.html">
                <i class="fa fa-pie-chart w-5 h-5 mr-3"></i>
                <span>库存分析</span>
            </a>

            <div class="px-4 mt-6 mb-2">
                <h2 class="text-xs font-semibold text-gray-400 uppercase tracking-wider">系统设置</h2>
            </div>

            <a href="salesman.html" class="sidebar-item" data-path="salesman.html">
                <i class="fa fa-user-circle w-5 h-5 mr-3"></i>
                <span>用户管理</span>
            </a>

            <a href="system/logs.html" class="sidebar-item active" data-path="system/logs.html">
                <i class="fa fa-history w-5 h-5 mr-3"></i>
                <span>数据库备份与恢复</span>
            </a>
        </nav>
    </aside>

    <!-- 主内容区域 -->
    <main class="flex-1 overflow-y-auto bg-gray-50 p-6">
        <div class="container mx-auto">
            <!-- 页面标题 -->
            <div class="mb-6">
                <h2 class="text-[clamp(1.5rem,3vw,2rem)] font-bold text-gray-800">数据库备份与恢复</h2>
                <p class="text-gray-500 mt-1">在这里进行数据库的备份和恢复操作</p>
            </div>

            <!-- 备份与恢复卡片 -->
            <div class="grid grid-cols-1 md:grid-cols-2 lg:grid-cols-2 gap-6 mb-8">
                <div class="bg-white rounded-xl p-6 card-shadow">
                    <div class="flex justify-between items-start">
                        <div>
                            <p class="text-gray-500 text-sm">数据库备份</p>
                            <h3 class="text-3xl font-bold mt-1 text-gray-800">立即备份</h3>
                            <p class="text-gray-500 mt-2">点击按钮进行数据库备份操作，备份文件将保存到指定位置。</p>
                            <!-- 备份按钮 -->
                            <button id="backupButton" class="mt-4 px-6 py-2 bg-primary text-white rounded-md hover:bg-primary/90 transition-colors">
                                <i class="fa fa-download mr-2"></i> 备份数据库
                            </button>
                        </div>
                        <div class="p-4 bg-blue-100 rounded-lg">
                            <i class="fa fa-database text-primary text-2xl"></i>
                        </div>
                    </div>
                </div>

                <div class="bg-white rounded-xl p-6 card-shadow">
                    <div class="flex justify-between items-start">
                        <div>
                            <p class="text-gray-500 text-sm">数据库恢复</p>
                            <h3 class="text-3xl font-bold mt-1 text-gray-800">选择文件恢复</h3>
                            <p class="text-gray-500 mt-2">选择备份文件进行数据库恢复操作，请谨慎操作，恢复后数据将被覆盖。</p>

                            <!-- 自定义文件选择器 -->
                            <div class="mt-6">
                                <label class="block text-sm font-medium text-gray-700 mb-2">选择 SQL 备份文件</label>

                                <!-- 自定义按钮 -->
                                <div class="flex items-center space-x-4">
                                    <button
                                            type="button"
                                            onclick="document.getElementById('backupFile').click()"
                                            class="px-5 py-2 bg-primary text-white rounded-md hover:bg-primary/90 transition-colors flex items-center shadow-sm">
                                        <i class="fa fa-folder-open mr-2"></i>
                                        选择文件
                                    </button>

                                    <!-- 显示已选文件名 -->
                                    <span id="fileNameDisplay" class="text-sm text-gray-600">未选择文件</span>
                                </div>

                                <!-- 原始 input file（隐藏） -->
                                <input
                                        type="file"
                                        id="backupFile"
                                        name="backupFile"
                                        accept=".sql"
                                        class="hidden"
                                        onchange="document.getElementById('fileNameDisplay').textContent = this.files.length ? this.files[0].name : '未选择文件'">
                            </div>

                            <!-- 恢复按钮 -->
                            <button id="restoreButton" class="mt-4 px-6 py-2 bg-primary text-white rounded-md hover:bg-primary/90 transition-colors">
                                <i class="fa fa-upload mr-2"></i> 恢复数据库
                            </button>
                        </div>
                        <div class="p-4 bg-green-100 rounded-lg">
                            <i class="fa fa-database text-success text-2xl"></i>
                        </div>
                    </div>
                </div>
            </div>

            <!-- 最近备份与恢复记录 -->
            <div class="bg-white rounded-xl p-6 card-shadow">
                <div class="flex justify-between items-center mb-6">
                    <h3 class="font-semibold text-gray-800">最近备份与恢复记录</h3>
                    <button class="text-primary text-sm">查看全部</button>
                </div>
                <div class="overflow-x-auto">
                    <div id="operations-loader" class="py-12 text-center">
                        <div class="inline-block animate-spin rounded-full h-12 w-12 border-b-2 border-primary"></div>
                        <p class="mt-4 text-gray-600">加载操作记录...</p>
                    </div>
                    <table class="w-full hidden" id="recent-operations">
                        <thead>
                        <tr class="border-b border-gray-200">
                            <th class="py-3 text-left text-sm font-medium text-gray-500">操作类型</th>
                            <th class="py-3 text-left text-sm font-medium text-gray-500">操作时间</th>
                            <th class="py-3 text-left text-sm font-medium text-gray-500">操作结果</th>
                        </tr>
                        </thead>
                        <tbody>
                        <!-- 最近操作数据将通过JavaScript动态填充 -->
                        </tbody>
                    </table>
                    <div id="operations-error" class="py-12 text-center hidden">
                        <i class="fa fa-exclamation-circle text-3xl text-warning mb-2"></i>
                        <p class="text-gray-600">加载操作记录失败，请刷新页面重试</p>
                    </div>
                </div>
            </div>
        </div>
    </main>
</div>

<!-- 页脚 -->
<footer class="bg-white py-4 border-t border-gray-200">
    <div class="container mx-auto px-4 text-center text-gray-500 text-sm">
        <p>© 2025 商店进销存管理系统 - 版权所有</p>
    </div>
</footer>

<!-- JavaScript -->
<script>
    // 页面加载完成后初始化
    document.addEventListener('DOMContentLoaded', async () => {
        try {
            // 显示加载状态
            const loader = document.getElementById('operations-loader');
            const table = document.getElementById('recent-operations');
            const error = document.getElementById('operations-error');

            loader.classList.remove('hidden');
            table.classList.add('hidden');
            error.classList.add('hidden');

            // 从后端API获取操作记录
            const response = await fetch('/admin/backup/history');

            if (!response.ok) {
                throw new Error(`HTTP error! status: ${response.status}`);
            }

            const data = await response.json();
            const operations = data.data;

            // 隐藏加载状态，显示表格
            loader.classList.add('hidden');
            table.classList.remove('hidden');

            // 动态填充最近操作记录
            const tableBody = document.querySelector('#recent-operations tbody');

            // 清空表格
            tableBody.innerHTML = '';

            // 如果没有记录，显示空状态
            if (operations.length === 0) {
                const emptyRow = document.createElement('tr');
                emptyRow.innerHTML = `
                    <td colspan="3" class="py-6 text-center text-gray-500">
                        <i class="fa fa-inbox text-2xl mb-2"></i>
                        <p>暂无备份与恢复记录</p>
                    </td>
                `;
                tableBody.appendChild(emptyRow);
                return;
            }

            // 渲染记录
            operations.forEach(op => {
                const row = document.createElement('tr');
                row.className = 'border-b border-gray-100 hover:bg-gray-50 transition-colors';

                const type = op.operationType === 'BACKUP' ? '备份' : '恢复';
                const result = op.status === 'SUCCESS' ? '成功' : '失败';
                const time = op.startTime; // 或者格式化时间，比如只显示日期或更友好的格式

                const typeCell = document.createElement('td');
                typeCell.className = 'py-3 px-4';
                typeCell.innerHTML = `<span class="px-2 py-1 rounded-full text-xs ${
                    op.operationType === 'BACKUP' ? 'bg-blue-100 text-blue-800' : 'bg-green-100 text-green-800'
                }">${type}</span>`;

                const timeCell = document.createElement('td');
                timeCell.className = 'py-3 px-4 text-gray-600';
                timeCell.textContent = new Date(op.startTime).toLocaleString(); // 更友好地显示时间

                const resultCell = document.createElement('td');
                resultCell.className = 'py-3 px-4';
                resultCell.innerHTML = `<span class="px-2 py-1 rounded-full text-xs ${
                    op.status === 'SUCCESS' ? 'bg-green-100 text-green-800' : 'bg-red-100 text-red-800'
                }">${result}</span>`;

                row.appendChild(typeCell);
                row.appendChild(timeCell);
                row.appendChild(resultCell);
                tableBody.appendChild(row);
            });
        } catch (error) {
            console.error('加载操作记录失败:', error);
            // 显示错误状态
            document.getElementById('operations-loader').classList.add('hidden');
            document.getElementById('operations-error').classList.remove('hidden');
        }
    });

    // 备份按钮事件监听器
    const backupButton = document.getElementById('backupButton');
    backupButton.addEventListener('click', async () => {
        try {
            const response = await fetch('/admin/backup', {
                method: 'POST',
                headers: {
                    'Content-Type': 'application/json'
                }
            });

            if (!response.ok) {
                throw new Error(`HTTP error! status: ${response.status}`);
            }

            const data = await response.text();
            alert(data);
            location.reload(true);
        } catch (error) {
            console.error('备份失败:', error);
            alert('备份失败，请重试');
        }
    });

    // 恢复按钮事件监听器
    const restoreButton = document.getElementById('restoreButton');
    restoreButton.addEventListener('click', async () => {
        const backupFileInput = document.getElementById('backupFile');
        const backupFile = backupFileInput.files[0];

        if (!backupFile) {
            alert('请选择备份文件');
            return;
        }

        try {
            const formData = new FormData();
            formData.append('backupFile', backupFile.name);

            const response = await fetch('/admin/backup/restore', {
                method: 'POST',
                body: formData
            });

            if (!response.ok) {
                throw new Error(`HTTP error! status: ${response.status}`);
            }

            const data = await response.text();
            alert(data);
            location.reload(true);
        } catch (error) {
            console.error('恢复失败:', error);
            alert('恢复失败，请重试');
        }
    });
</script>
</body>

</html>